package com.taotao.service.Impl;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.service.ContentCategoryService;


@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;
	
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_CONTENT_CAT_SYN_URL}")
	private String REST_CONTENT_CAT_SYN_URL;
	
	@Override
	public List<TbContentCategory> getListByParentId(Long pid) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		example.createCriteria().andParentIdEqualTo(pid);
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		return list;
	}

	@Override
	public TaotaoResult updateNameById(Long id, String name) {
		if(name==null || name.trim().isEmpty())
			return TaotaoResult.build(400,  "错误的参数");
		
		TbContentCategory content = contentCategoryMapper.selectByPrimaryKey(id);
		content.setId(id);
		content.setName(name);
		
		int n = contentCategoryMapper.updateByPrimaryKeySelective(content);
		if(n==0)
			return TaotaoResult.build(401,  "Id为"+id+"的该记录不存在");
		
		HttpClientUtil.doGet(REST_BASE_URL+REST_CONTENT_CAT_SYN_URL+"/"+id);
		
		return TaotaoResult.ok();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public TaotaoResult createContentCategory(Long parentId, String name) {
		if(parentId==null || name==null)
			TaotaoResult.build(402,  "错误的值");
		TbContentCategory record=new TbContentCategory();
		record.setParentId(parentId);
		record.setName(name);
		record.setStatus(1);
		record.setSortOrder(1);
		Date date = new Date();
		record.setIsParent(false);
		record.setUpdated(date);
		record.setCreated(date);
		contentCategoryMapper.insert(record);
		
		return TaotaoResult.ok(new HashMap().put("id", record.getId()));
	}

	@Override
	public TaotaoResult removeContentCategory(Long parentId, Long id) {
		if(parentId==null || id==null)
			return TaotaoResult.build(402,  "错误的值");
		
		TbContentCategoryExample example=new TbContentCategoryExample();
		example.createCriteria().andParentIdEqualTo(parentId);
		List<TbContentCategory> brothers = contentCategoryMapper.selectByExample(example);
		if(brothers.size()==1) {
			TbContentCategory brother = brothers.get(0);
			brother.setIsParent(false);
			contentCategoryMapper.updateByPrimaryKeySelective(brother);
		}
		contentCategoryMapper.deleteByPrimaryKey(id);
		
		return TaotaoResult.ok();
	}

}
